#include <commonheader.h>
#include "baseserver.h"

CBaseServer::CBaseServer()
{
    Reset();
}

CBaseServer::~CBaseServer()
{
}

void CBaseServer::Reset()
{
    m_dwSvrID = 0;
    m_poPipe = NULL;
    memset(m_szSvrID, 0, SVR_ID_LEN);
    m_poPkgHandle = NULL;
}

void CBaseServer::Init(UINT32 dwSvrID, ISDPipe *poPipe, ISDSvrHandler * poPkgHandle)
{
    SetID(dwSvrID);
    m_poPipe = poPipe;
    m_poPkgHandle = poPkgHandle;
}

void CBaseServer::OnRecv(UINT16 wBusinessID, const char *pData, UINT32 dwLen)
{
    if (NULL == m_poPkgHandle)
    {
        return;
    }

    if(wBusinessID != m_poPkgHandle->GetBusinessID())
    {
        SYS_IMPORTANT("CBaseServer::OnRecv Error! business ID %d, ServerID %s", wBusinessID, m_szSvrID);
        return;
    }

    m_poPkgHandle->ProcessPacket(m_dwSvrID, pData, dwLen);
}

void CBaseServer::OnReport(UINT16 wBusinessID, INT32 nErrCode)
{
    SYS_IMPORTANT("CBaseServer::OnReport! business ID %d, errcode %d, ServerID %s", wBusinessID, nErrCode, m_szSvrID);
}

bool CBaseServer::Send( const char* pData, UINT32 dwLen)
{
    if (NULL == m_poPipe || NULL == m_poPkgHandle)
        return false;

    return m_poPipe->Send(m_poPkgHandle->GetBusinessID(), pData, dwLen);
}

void CBaseServer::OnConnected()
{
    SYS_IMPORTANT("CBaseServer Connected Success! SvrID %s", m_szSvrID);
    if (NULL != m_poPkgHandle)
    {
        m_poPkgHandle->OnConnected(m_dwSvrID);
    }
}

void CBaseServer::OnDisConnected(INT32 nErrCode)
{
    SYS_IMPORTANT("CBaseServer DisConnected! SvrID %s, ErrorCode %d", m_szSvrID, nErrCode);
    if (NULL != m_poPkgHandle)
    {
        m_poPkgHandle->OnDisConnected(m_dwSvrID, nErrCode);
    }
}
